﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Core;
using Tanmia.Basis.Domain;

namespace Tanmia.Domain
{
    public enum AccountTools { Person = 0, Account = 1 }

    public class AccountDomain : BaseDomainObject<Account>
    {
        public virtual IList<Account> GetsWithAccountTools(AccountTools accountTools)
        {
            if (accountTools == AccountTools.Person)
                return GetsWithSelect(Gets(), Account.NullAccount, Account.NewAccount, Account.SelectAccount);
            else
                return GetsWithSelect(Gets(), Account.NullAccount, Account.SelectAccount);
        }

        public Account GetByNumber(string accountNumber)
        {
            return repository.Gets(x => x.Number == accountNumber).FirstOrDefault();
        }

        public IList<Account> GetsWithSelect()
        {
            return GetsWithSelect(Gets(), Account.SelectAccount);
        }

        public string GetMaxAccount(Account parent)
        {
            string reVal;

            var accountList = repository.Gets(x => x.Parent == parent);

            if (accountList.Count == 0)
            {
                reVal =  parent.Number;
                reVal += 1;
                return reVal;
            }

            reVal = accountList[accountList.Count - 1].Number;
            int y = int.Parse(reVal);
            y++;
            reVal = y.ToString();

            return reVal;
        }
    }
}
